/*
 * Copyright (c) 2010 Scott Morgan
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */

package captaintest;

import captaintest.emma.EmmaParser;
import captaintest.util.Assert;

import java.io.File;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

public class Captain {

    private File historyFile;
    private File coverageFile;
    private String coverageFormat = "emma";

    public Captain(File historyFile, File coverageFile) {
        this(historyFile, coverageFile, "emma");
    }

    public Captain(File historyFile, File coverageFile, String coverageFormat) {
        this.historyFile = historyFile;
        this.coverageFile = coverageFile;
        this.coverageFormat = coverageFormat;
    }

    public void setHistoryFile(File historyFile) {
        this.historyFile = historyFile;
    }

    public void setCoverageFile(File coverageFile) {
        this.coverageFile = coverageFile;
    }

    public List<FileCoverage> run(){
        Assert.notNull(historyFile);
        Assert.notNull(coverageFile);
        Assert.notNull(coverageFormat);
        Assert.isTrue(!coverageFile.equals(historyFile),
                "Cannot use the same file for coverage and history");

        EmmaParser ep = new EmmaParser(coverageFile);
        List<FileCoverage> lastResult = ep.getCoverageList();
        Map<String, FileCoverage> lastResultMap = convertListToMap(lastResult);

        XmlCoverageDAO dao = new XmlCoverageDAO();

        if (historyFile.exists()){
            List<FileCoverage> historyList = dao.load(historyFile);

            for (int i = 0; i < historyList.size(); i++) {
                FileCoverage fc1 = historyList.get(i);
                FileCoverage fc2 = lastResultMap.remove(fc1.getFileName());
                if (fc2 != null){
                    fc1.mergeFileCoverage(fc2);
                }
            }
            for (FileCoverage fileCoverage : lastResultMap.values()) {
                historyList.add(fileCoverage);
            }

            dao.save(historyFile, historyList);
            return historyList;
        } else {
            dao.save(historyFile, lastResult);
            return lastResult;
        }
    }

    private Map<String, FileCoverage> convertListToMap(List<FileCoverage> lastResult) {
        Map<String, FileCoverage> map = new TreeMap<String, FileCoverage>();
        for (FileCoverage coverage : lastResult) {
            map.put(coverage.getFileName(), coverage);
        }

        return map;
    }

    public static void main(String[] args) {
//        EmmaParser ep = new EmmaParser(new File("./core/data/emma/example.xml"));

        Captain cap = new Captain(new File(args[1]), new File(args[0]));
        cap.run();

//        EmmaParser ep1 = new EmmaParser(new File("./core/data/emma/coverage1.xml"));
//        EmmaParser ep2 = new EmmaParser(new File("./core/data/emma/coverage2.xml"));
//        List<FileCoverage> coverageList1  = ep1.getCoverageList();
//        List<FileCoverage> coverageList2  = ep2.getCoverageList();
//
//        for (int i = 0; i < coverageList1.size(); i++) {
//            FileCoverage fc1 = coverageList1.get(i);
//            FileCoverage fc2 = coverageList2.get(i);
//
//            fc2.mergeFileCoverage(fc1);
//        }
//
//        XmlCoverageDAO dao = new XmlCoverageDAO();
//        String xml = dao.toXML(coverageList2);
//        System.out.println(xml);

//        FileUtil.saveFile("test.xml", xml);
//
//        String readXml = FileUtil.readFile("test.xml");
//        System.out.println("readXml = " + readXml);
//
//        int newSize = dao.fromXML(xml).size();
//        System.out.println("newSize = " + newSize);

    }

}
